package com.rockwell.maze800.test;

import com.rockwell.maze800.utils.Speaker;

import android.os.AsyncTask;
import android.test.AndroidTestCase;

public class SpeakerTester extends AndroidTestCase {
	@Override
	public void setUp() {
	}

	@Override
	public void tearDown() {
	}

	static class WaitUntilSpeakerInitialized extends AsyncTask<Void, Void, Void> {
		private Speaker m_speaker;
		private Runnable m_runnable;
		
		public WaitUntilSpeakerInitialized(Speaker speaker, Runnable runnable) {
			m_speaker = speaker;
			m_runnable = runnable;
		}
		
		@Override
		protected Void doInBackground(Void... params) {
			while (!m_speaker.hasInitialized()) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return null;
		}
		
		protected void onPostExecute(Void p) {
			if (m_runnable != null) {
				m_runnable.run();
			}
		}
	}
	
	public void testSpeak() {
		final Speaker speaker = new Speaker(getContext());
		
		WaitUntilSpeakerInitialized task = new WaitUntilSpeakerInitialized(speaker, new Runnable() {

			@Override
			public void run() {
				assertTrue(speaker.canSpeak());
				
				speaker.speak("Speaker is working");
				speaker.shutDown();
			}
			
		});
		
		task.execute((Void[])null);
	}
}
